package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.product.model.Item;
import com.atguigu.gmall.product.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin")
@RefreshScope  //开启实时刷新配置
public class TestController {


    @Value("${refresh.config}")
    private String config;

    /**
     * 测试刷新配置
     *
     * @return
     */
    @GetMapping("/getConfig")
    public Result getConfig() {
        return Result.ok(config);
    }


    @PostMapping("item")
    public Result<Item> saveItem(Item item) {
        // 模拟业务执行出现异常
        int i = 1 / 0;
        // 保存业务
        System.out.println("保存成功");
        return Result.build(item, ResultCodeEnum.SUCCESS);
    }


    @Autowired
    private TestService testService;

    @GetMapping("/testLock")
    public Result testLock() {
        testService.testLock();
        return Result.ok();
    }


    @GetMapping("/write")
    public Result write() {
        testService.write();
        return Result.ok();
    }

    @GetMapping("/read")
    public Result read() {
        testService.read();
        return Result.ok();
    }


    @GetMapping("/async")
    public Result async(String data) {
        testService.async(data);
        System.err.println(Thread.currentThread().getName());
        return Result.ok();
    }
}
